
import { Address } from '../../utils/address.js';
import { Order } from '../order/order-model.js';
import { My } from '../my/my-model.js';

var address = new Address();
var order = new Order();
var my = new My();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    orderArr: [],
    isLoadedAll: false,
    session3rd: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 从缓存中取出3rd_session
    wx.getStorage({
      key: '3rd_session',
      success: function (res) {
        that.setData({
          session3rd: res.data
        })
        that._loadData(res.data);   // 获取订单信息
        that._getAddressInfo(res.data);     // 获取地址信息
      },
    })

  },

  onShow: function () {
    // var newOrderFlag = order.hasNewOrder();
    // if (newOrderFlag) {
    //   console.log('onshow');
    //   this.refresh();
    // }

    // 因为每次支付后跳转到my页面  所以需要每次刷新订单数据
    // 第一次进来  从缓存中获取session  之后直接读取data
    const that = this
    const session3rd = this.data.session3rd

    if (session3rd){
      that._getOrders(session3rd)
    } else {
      wx.getStorage({
        key: '3rd_session',
        success: function (res) {
          that._getOrders(res.data)
        }
      })
    }
    
  },

  refresh: function () {
    var that = this;
    this.data.orderArr = [];  //订单初始化
    this._getOrders(() => {
      console.log('refresh');
      that.data.isLoadedAll = false;  //是否加载完全
      that.data.pageIndex = 1;
      order.execSetStorageSync(false);  //更新标志位
    });
  },
  // 获取当前地址信息
  _getAddressInfo(session3rd) {

    address.getAddress((res) => {
      this._bindAddressInfo(res.data);
    }, session3rd);

  },

  /*绑定地址信息*/
  _bindAddressInfo: function (addressInfo) {

    const info = {
      name: addressInfo.contact,
      mobile: addressInfo.phone,
      totalDetail: addressInfo.address_info + addressInfo.address
    }

    this.setData({
      addressInfo: info
    })

  },

  _loadData: function (session3rd) {

    my.getUserInfo((data) => {
      this.setData({
        userInfo: data
      });
    });

    this._getOrders(session3rd);
  },

   /*_getOrders: function (callback) {
    order.getOrders(this.data.pageIndex, (res) => {
      var data = res.data;

      if (data.length > 0) {
        this.data.orderArr.push.apply(this.data.orderArr, data);
        this.setData({
          orderArr: this.data.orderArr
        });
      }
      else {
        this.data.isLoadedAll = true;
      }
      callback && callback();
    })
   },*/

  _getOrders(session3rd) {
    order.getOrders(session3rd, (res) => {
      if (res.order.length > 0) {
        this.setData({
          orderArr: res.order
        })
      }
    })
  },
  // 上拉加载
  // onReachBottom: function () {
  //   if (!this.data.isLoadedAll) {
  //     this.data.pageIndex++;
  //     this._getOrders();
  //     //scroll-view
  //   }
  // },

  /*显示订单的具体信息*/
  showOrderDetailInfo: function (event) {

    // var id = order.getDataSet(event, 'id');
    // console.log(id);
    // wx.navigateTo({
    //   url: '../order/order?from=order&id=' + id
    // });
  },

  // 未付款的支付
  rePay: function (event) {
    var id = order.getDataSet(event, 'id'),
      ordernum = order.getDataSet(event, 'ordernum'),
      index = order.getDataSet(event, 'index');
    const session3rd = this.data.session3rd

    this._execPay(id, index, ordernum, session3rd);
  },

  _execPay: function (id, index, ordernum, session3rd) {
    var that = this;

    const opations = {
      session3rd,
      sn:ordernum
    }
    
    /*order.execPay(id, (statusCode) => {

      if (statusCode > 0) {
        var flag = statusCode == 2;

        //更新订单显示状态
        if (flag) {
          that.data.orderArr[index].status = 2;
          that.setData({
            orderArr: that.data.orderArr
          });
        }

        //跳转到 成功页面
        wx.navigateTo({
          url: `../pay-result/pay-result?&flag=${flag}&from=my`
        });
      }

      else {
        that.showTips('支付失败', '商品已下架或库存不足');
      }

    })*/

    order.execPay(opations, (statusCode) => {
      console.log(statusCode)
      var flag
      if (statusCode === 1){
        //跳转到 成功页面
        flag = true
        wx.navigateTo({
          url: `../pay-result/pay-result?&flag=${flag}&from=my`
        });
      } else {
        //跳转到 支付失败页面
        flag = false
        wx.navigateTo({
          url: `../pay-result/pay-result?&flag=${flag}&from=my`
        });
      }
    })

  },

  /*
 * 提示窗口
 * params:
 * title - {string}标题
 * content - {string}内容
 * flag - {bool}是否跳转到 "我的页面"
 */
  showTips: function (title, content) {
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
      success: function (res) {

      }
    });
  },
  // 地址管理
  editAddress(event) {
    console.log(111)
    var that = this;

    wx.chooseAddress({
      success: function (res) {
        console.log(res);
        // var addressInfo = {
        //   name: res.userName,
        //   mobile: res.telNumber,
        //   totalDetail: address.setAddressInfo(res)
        // }
        const info = {
          contact: res.userName,
          phone: res.telNumber,
          address_info: res.provinceName + res.cityName + res.countyName,
          address: res.detailInfo
        }
        that._bindAddressInfo(info);

        // 保存地址  更新后台地址信息
        const session3rd = that.data.session_3rd
        const options = {
          session3rd,
          contact: res.userName,
          phone: res.telNumber,
          address_info: res.provinceName + res.cityName + res.countyName,
          address: res.detailInfo,
          postcodes: res.postalCode
        }

        address.submitAddress(options, (res) => {
          if (!res.status) {
            that.showTips('操作提示', '地址信息更新失败！');
          }
        });
      }
    })
  }




})